package com.bestcem.xm.ticket.controller.vo.ticket.config;

import com.bestcem.xm.ticket.entity.mongo.TicketFlow;
import com.bestcem.xm.ticket.enums.TicketFlowMatchConditionEnum;
import com.bestcem.xm.ticket.enums.TicketFlowStatusEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
public class TicketConfigFlowVO {
    @JsonProperty(value = "id")
    private String id;

    /**
     * orgId
     */
    @JsonProperty(value = "orgId")
    private String orgId;

    /**
     * userId
     */
    @JsonProperty(value = "userId")
    private String userId;

    /**
     * ticketConfigId
     */
    @JsonProperty(value = "ticketConfigId")
    private String ticketConfigId;

    /**
     * flowId 工单流程序列号
     */
    @JsonProperty(value = "flowId")
    private Integer flowId;

    /**
     * title
     */
    @JsonProperty(value = "title")
    private String title;

    /**
     * status
     *
     * @see TicketFlowStatusEnum
     */
    @JsonProperty(value = "status")
    private Integer status;

    /**
     * matchCondition
     *
     * @see TicketFlowMatchConditionEnum
     */
    @JsonProperty(value = "matchCondition")
    private Integer matchCondition;

    /**
     * alertRuleIds ObjectId 数组
     */
    @JsonProperty(value = "alertRuleIds")
    private List<String> alertRuleIds;

    /**
     * customRuleIds
     */
    @JsonProperty(value = "customRuleIds")
    private List<CustomRuleId> customRuleIds;

    /**
     * priority 工单流程的优先级 数字越小优先级越高
     */
    @JsonProperty(value = "priority")
    private Integer priority;

    /**
     * 保存每个跟进人的详细配置, 注意数组内元素的顺序, 第一个元素就是第一跟进人, 第二个就是第二跟进人, 最多两个跟进人
     */
    @JsonProperty(value = "followConfig")
    private List<FollowConfig> followConfigs;

    /**
     * 工单是否需要审核
     */
    @JsonProperty(value = "needAudit")
    private Boolean needAudit;

    /**
     * auditConfig
     */
    @JsonProperty(value = "auditConfig")
    private List<FollowConfig> auditConfigs;

    /**
     * version
     */
    @JsonProperty(value = "version")
    private Integer version;

    /**
     * 工单流程规则列表
     */
    @JsonProperty(value = "ruleList")
    private List<String> ruleList;


    /**
     * CustomRuleId
     */
    @Data
    public static class CustomRuleId {

        /**
         * id
         */
        @JsonProperty(value = "id")
        private String id;

        /**
         * title
         */
        @JsonProperty(value = "title")
        private String title;

        @Nullable
        public static CustomRuleId getInstance(TicketFlow.CustomRuleId entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            CustomRuleId data = new CustomRuleId();
            data.setId(entity.getId());
            data.setTitle(entity.getTitle());
            return data;
        }
    }

    /**
     * FollowConfig
     */
    @Data
    public static class FollowConfig {

        /**
         * title
         */
        @JsonProperty(value = "title")
        private String title;

        /**
         * follower_email
         */
        @JsonProperty(value = "follower_email")
        private String followerEmail;

        /**
         * follower_id
         */
        @JsonProperty(value = "follower_id")
        private String followerId;

        /**
         * follower_name
         */
        @JsonProperty(value = "follower_name")
        private String followerName;

        /**
         * follower_filter
         */
        @JsonProperty(value = "follower_filter")
        private Integer followerFilter;

        /**
         * region
         */
        @JsonProperty(value = "region")
        private String region;

        /**
         * roleId
         */
        @JsonProperty(value = "roleId")
        private String roleId;

        /**
         * timeLimitType
         */
        @JsonProperty(value = "timeLimitType")
        private Integer timeLimitType;

        /**
         * timeLimit
         */
        @JsonProperty(value = "timeLimit")
        private String timeLimit;

        /**
         * overdueHandle
         */
        @JsonProperty(value = "overdueHandle")
        private Integer overdueHandle;

        /**
         * emptyFollowerHandle
         */
        @JsonProperty(value = "emptyFollowerHandle")
        private Integer emptyFollowerHandle;

        /**
         * options
         */
        @JsonProperty(value = "options")
        private List<Option> options;


        @Data
        public static class Option {

            /**
             * id
             */
            @JsonProperty(value = "id")
            private String id;

            /**
             * title
             */
            @JsonProperty(value = "title")
            private String title;

            /**
             * role_index
             */
            @JsonProperty(value = "role_index")
            private String roleIndex;

            /**
             * roleType
             */
            @JsonProperty(value = "roleType")
            private Integer roleType;

            @Nullable
            public static Option getInstance(TicketFlow.FollowConfig.Option entity) {
                if (Objects.isNull(entity)) {
                    return null;
                }
                Option data = new Option();
                data.setId(entity.getId());
                data.setTitle(entity.getTitle());
                data.setRoleIndex(entity.getRoleIndex());
                data.setRoleType(entity.getRoleType());
                return data;
            }
        }

        @Nullable
        public static FollowConfig getInstance(TicketFlow.FollowConfig entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            FollowConfig data = new FollowConfig();
            data.setTitle(entity.getTitle());
            data.setFollowerEmail(entity.getFollowerEmail());
            data.setFollowerId(entity.getFollowerId());
            data.setFollowerName(entity.getFollowerName());
            data.setFollowerFilter(entity.getFollowerFilter());
            data.setRegion(entity.getRegion());
            data.setRoleId(entity.getRoleId());
            data.setTimeLimitType(entity.getTimeLimitType());
            data.setTimeLimit(entity.getTimeLimit());
            data.setOverdueHandle(entity.getOverdueHandle());
            data.setEmptyFollowerHandle(entity.getEmptyFollowerHandle());

            List<Option> optionList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(entity.getOptions())) {
                List<TicketFlow.FollowConfig.Option> options = entity.getOptions();
                for (TicketFlow.FollowConfig.Option option : options) {
                    optionList.add(Option.getInstance(option));
                }
            }
            data.setOptions(optionList);

            return data;
        }
    }

    @JsonProperty(value = "record_flag")
    private Integer recordFlag = 1;

    @JsonProperty("created_id")
    private String createdId;

    @JsonProperty("created_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @JsonProperty("updated_id")
    private String updatedId;

    @JsonProperty("updated_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedDate;
}
